package cn.thoughtworks.service;

import cn.thoughtworks.error.ErrorMessage;
import cn.thoughtworks.model.Size;
import cn.thoughtworks.service.impl.IncorrectCommand;
import cn.thoughtworks.service.impl.InvalidNumber;
import cn.thoughtworks.service.impl.MazeError;
import cn.thoughtworks.service.impl.OutOfRange;

/**
 * 检查输入有效性
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @create 2018-07-27-15:57
 */
public class CheckService {





    public String check(Size size,String command){
        //1. 验证无效数字
        Verify invalidNumber = new InvalidNumber();
        if (!invalidNumber.verify(size,command))
            return ErrorMessage.INVALID_NUMBER.msg;

        //2. 验证格式错误
        IncorrectCommand incorrectCommand = new IncorrectCommand();
        if (!incorrectCommand.verify(size,command))
            return ErrorMessage.INCORRECT_COMMAND.msg;

        //3. 验证数字是否超出预定范围
        OutOfRange outOfRange = new OutOfRange();
        if (!outOfRange.verify(size,command))
            return ErrorMessage.OUT_OF_RANGE.msg;


        //4. 验证连通性错误
        MazeError mazeError = new MazeError();
        if (!mazeError.verify(size,command))
            return ErrorMessage.MAZE_ERROR.msg;

        //验证通过
        return null;
    }

    /**
     * 验证网格大小的输入数字是否合法
     * @param input 大小输入
     * @return Size类
     */
    public Size checkSize(String input){
        String[] sizes = input.split(" ");
        if (sizes.length != 2)
            return null;
//        String regex = "^([1-9]\\d*\\.?\\d*)|(-?0\\.\\d*[1-9])|(-?[0])|(-?[0]\\.\\d*)$";
        String regex = "^[1-9]\\d*$";

        if (sizes[0].matches(regex)&&sizes[1].matches(regex)) {
            return new Size(Integer.valueOf(sizes[0]),Integer.valueOf(sizes[1]));
        }

        return null;
    }
}
